//
//  ViewController.m
//  RV获取JSON数据_10-19
//
//  Created by 伍银荣 on 2017/10/19.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "ViewController.h"
#import "bookModel.h"
#import "WordsTableViewController.h"
#import "WordsViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *myTableView;
@property (nonatomic,strong) NSArray *bookArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //拿到文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"TABLE_META.json" ofType:nil];
    //将数据转化成NSData类型
    NSData *data = [NSData dataWithContentsOfFile:path];
    //将data数据转化成字典类型
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSLog(@"-----arr:%@",arr);
    NSMutableArray *marr = [[NSMutableArray alloc]init];
    for (NSDictionary *dict in arr) {
        bookModel *bookItem = [[bookModel alloc]initWithDict:dict];
        [marr addObject:bookItem];
    }
    _bookArr = marr;
    NSLog(@"*****bookArr:%@",_bookArr);
    
    [self setupUI];
}

-(void)setupUI
{
    [self.view addSubview:self.myTableView];
    //[_myTableView reloadData];
}

-(UITableView *)myTableView
{
    if (!_myTableView) {
        UITableView *myTableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        _myTableView = myTableView;
    }
    return _myTableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _bookArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.textLabel.text = [_bookArr[indexPath.row] Meta_Name];
    cell.detailTextLabel.text = [_bookArr[indexPath.row] Meta_Key];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    WordsTableViewController *wordsTableVC = [[WordsTableViewController alloc]init];
//    wordsTableVC.title = [_bookArr[indexPath.row] Meta_Key];
//    wordsTableVC.key = wordsTableVC.title;
//    [self presentViewController:wordsTableVC animated:YES completion:nil];
    WordsViewController *wordsVC = [[WordsViewController alloc]init];
    wordsVC.title = [_bookArr[indexPath.row] Meta_Key];
    wordsVC.key = wordsVC.title;
    [self presentViewController:wordsVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
